//
//  Savefile.m
//  Keyvaluecoding
//
//  Created by 赵洋 on 14/11/7.
//  Copyright (c) 2014年 赵洋. All rights reserved.
//

#import "Savefile.h"

@implementation Savefile
// 根据名字获取路径。library
+ (NSString *)pathWithName:(NSString *)name
{
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) ;
    NSString *path = [arr lastObject];
    path = [NSString stringWithFormat:@"%@/%@", path, name];
    return path;
}
// 判断文件是否存在
+ (BOOL)judgeFileExistWithName:(NSString *)name
{
    NSString *path = [Savefile pathWithName:name];
    NSFileManager *mana = [NSFileManager defaultManager];
    if ([mana fileExistsAtPath:path]) {
        return YES;
    } else {
        return NO;
    }
}

+ (NSData *)readFileWithName:(NSString *)name
{
    if ([Savefile judgeFileExistWithName:name]) {
        NSString *path = [Savefile pathWithName:name];
        NSData *data = [NSData dataWithContentsOfFile:path];
        return data;
    } else {
        return nil;
    }
}

+ (BOOL)writeFileWithName:(NSString *)name data:(NSData *)data
{
    NSString *path = [Savefile pathWithName:name];
    return [data writeToFile:path atomically:YES];
}

@end
